# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=tk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=8.6.12
_patch=
pkgver=${_basever}${_patch}
pkgrel=2
pkgdesc="A windowing toolkit for use with tcl (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://tcl.sourceforge.io/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-tcl>=${_basever}")
options=('staticlibs') # '!strip' 'debug')
source=("https://downloads.sourceforge.net/sourceforge/tcl/${_realname}${pkgver}-src.tar.gz"
        003-fix-forbidden-colon-in-paths.mingw.patch
        004-install-man.mingw.patch
        005-install-pkgconf.mingw.patch
        006-prevent-tclStubsPtr-segfault.patch
        008-dont-link-shared-with--static-libgcc.patch
        010-win-non-x86.patch
        011-win-arm64.patch)
sha256sums=('12395c1f3fcb6bed2938689f797ea3cdf41ed5cb6c4766eec8ac949560310630'
            '3282a5f5fbcc932bb12b82a0b658f69187c90b1018917d05d579fa1b8868e4a8'
            'e6e843eff45b2cd2e371bb732209a0d65dcb23ddcd51bc1d2e372efc071a6c37'
            'b968a0fdeacb01f920759fa7e04628a2b08a22b5db4ae9022dfae153e0a3d36a'
            '2d3d5ac70ed8e92abf441b3e0b7ef94996fc9c2fe32f3d455974012fba166b7c'
            '228bbd94b5c5a362032e70d363cd04ff20e851663973ddbceebf5f235717d669'
            '68fd7a808b0cc31946697e7d52efb945faa44953ed44a08645c22222a6018139'
            '4f4d38217a6db3c0b318c85bb00b3aae6ed715c8da66d41ffae6a91d375aa971')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}${_basever}"
  _apply_patch_with_msg \
    003-fix-forbidden-colon-in-paths.mingw.patch \
    004-install-man.mingw.patch \
    005-install-pkgconf.mingw.patch \
    006-prevent-tclStubsPtr-segfault.patch \
    008-dont-link-shared-with--static-libgcc.patch \
    010-win-non-x86.patch \
    011-win-arm64.patch

  cd win && autoreconf -fi
}

build() {
   local extra_config=
  if check_option "debug" "y"; then
    extra_config="--enable-symbols"
  fi
  local enable64bit=
  [[ "${MINGW_CHOST}" = 'x86_64-w64-mingw32' ]] && enable64bit='--enable-64bit'
  [[ "${MINGW_CHOST}" = 'aarch64-w64-mingw32' ]] && enable64bit='--enable-64bit=arm64'

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM}
  cd "${srcdir}/build-${MSYSTEM}"
  ${srcdir}/tk${_basever}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-tcl=${MINGW_PREFIX}/lib \
    ${extra_config} \
    ${enable64bit}

  make
}

package() {
  local debug_suffix=
  if check_option "debug" "y"; then
    debug_suffix="g"
  fi
  cd "${srcdir}/build-${MSYSTEM}"
  make install INSTALL_ROOT="${pkgdir}"
  cp "${pkgdir}${MINGW_PREFIX}/bin/wish86${debug_suffix}.exe" "${pkgdir}${MINGW_PREFIX}/bin/wish.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtk86${debug_suffix}.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtk.dll.a"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/"{generic/ttk,win}

  cd ${srcdir}/tk${_basever}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tk${_basever%.*}/tk-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for header in *.h ; do
      cp -f ${header} ${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl

  local _libver=${_basever%.*}
  _libver=${_libver//./}
  sed -i \
    -e "s|^\(TK_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtk${_libver}.dll.a'|" \
    -e "s|^\(TK_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltkstub${_libver}'|" \
    ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  # Add missing entry to tkConfig.sh
  echo "# String to pass to the compiler so that an extension can" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "# find installed Tcl headers." >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "TK_INCLUDE_SPEC='-I${MINGW_PREFIX}/include/tk${_basever%.*}'" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  rm "${pkgdir}${MINGW_PREFIX}/lib/tk${_basever%.*}/tkAppInit.c"
}
